package luj.game.robot.internal.instance.boss.command.map;

import luj.ava.reflect.type.TypeX;
import luj.game.robot.api.action.RobotBossCommand;
import luj.game.robot.internal.dynamic.combine.AllBeanCombiner;

import java.util.Map;
import java.util.function.Function;

import static java.util.stream.Collectors.toMap;

public class BossCmdMapFactory {

  public BossCmdMapFactory(AllBeanCombiner.Result rootBean) {
    _rootBean = rootBean;
  }

  public BossCommandMap create() {
    Map<String, BossCommandMap.Cmd> cmdMap = _rootBean.bossCommand().stream()
        .map(this::makeCommand)
        .collect(toMap(MCommandImpl::getCommandName, Function.identity()));

    var result = new MapImpl();
    result._cmdMap = cmdMap;

    return result;
  }

  private MCommandImpl makeCommand(RobotBossCommand<?> cmdInst) {
    var cmd = new MCommandImpl();
    cmd._command = cmdInst;

    Class<?> cmdType = cmdInst.getClass();
    cmd._commandType = cmdType;

    cmd._paramType = TypeX.of(cmdType)
        .getSupertype(RobotBossCommand.class)
        .getTypeParam(0)
        .asClass();

    return cmd;
  }

  private final AllBeanCombiner.Result _rootBean;
}
